<?php

/**
 * convert actions.
 *
 * @package    juvenalis
 * @subpackage convert
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class convertActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  }

  public function executeConvert(sfWebRequest $request)
  {
	/* First we create a connection to the original database */
	$dbhost = 'localhost';
	$dbuser = 'juvenalis';
	$dbpass = 'secret';
	$dbname = 'juvenalis';

	$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
	mysql_select_db($dbname);

	/* Hash initialisation */
	$pricecodes = array();
	$types = array();
	$genres = array();

	/* Convert Price codes */
	$sth = mysql_query("SELECT * FROM prijscodes");

	while ($row = mysql_fetch_array($sth))
	{
		$pc = new Price();
		$pc->setName($row["type"]);
		$pc->save();

		$pricecodes[$row["id"]] = $pc->getId();
	}

	/* Convert types */
	$sth = mysql_query("SELECT DISTINCT(type) FROM verhuur WHERE type != ''");

	while ($row = mysql_fetch_array($sth))
	{
		$type = new Carrier();
		$type->setName($row["type"]);
		$type->Save();

		$types[strtoupper($row["type"])] = $type->getId();
	}

	$type = new Carrier();
	$type->setName("Geen type");
	$type->Save();
	$types["GEEN TYPE"] = $type->getId();

	/* Convert genres */
	$sth = mysql_query("SELECT * FROM genres WHERE genre != ''");

	while ($row = mysql_fetch_array($sth))
	{
		$genre = new Genre();
		
		$genre->setName($row["genre"]);
		$genre->Save();

		$genres[$row["id"]] = $genre->getId();

	}

	/* Convert rentals */
	$sth = mysql_query("SELECT * FROM verhuur");

	while ($row = mysql_fetch_array($sth))
	{
		$rental = new Rental();

		preg_match("/^(\D*)(\d*)$/",$row["nr"],$matches);

		$prefix = $matches[1];
		$suffix = $matches[2];

		$broken = 0;

		if ($row["defect"] == "Y")
		{
			$broken = 1;
		}

		if ($row["defect"] == "y")
		{
			$broken = 1;
		}

		if ($row["type"] == "")
		{
			$row["type"] = "Geen type";
		}

		if ($row["creatie"] == "")
		{
			$row["creatie"] = "1970-01-01";
		}

		if ($row["creatie"] == "--")
		{
			$row["creatie"] = "1970-01-01";
		}


		$rental->setPrefix($prefix);
		$rental->setSuffix($suffix);
		$rental->setTitle($row["titel"]);
		$rental->setDescription("");
		$rental->setBroken($broken);
		$rental->setDeleted(0);
		$rental->setTurnover($row["opbrengst"]);
		$rental->setRents($row["tot_verh"]);
		$rental->setNote($row["nota"]);
		$rental->setPlace($row["plaats"]);
		$rental->setPriceId($pricecodes[$row["prijscode"]]);
		$rental->setCarrierId($types[strtoupper($row["type"])]);
		$rental->setCreatedAt($row["creatie"] . " 12:00");
		$rental->setUpdatedAt($row["creatie"] . " 12:00");
		
		$rental->Save();

		# Now attach the genres
		$rental_id = $rental->getid();
		$old_id = $row["id"];

		$sth2 = mysql_query("SELECT gv.id_genre FROM genres_verhuur as gv,genres as g WHERE gv.id_verhuur=$old_id AND g.id=gv.id_verhuur AND g.genre != ''");
		while ($row2 = mysql_fetch_array($sth2))
		{
			$rg = new RentalGenre();
			$rg->setRentalId($rental_id);
			$rg->setGenreId($genres[$row2["id_genre"]]);
			$rg->save();
		}


	}
  }
}
